import java.util.Random;
import java.util.Scanner;

public class NGuessNumber {
    static Scanner scanner = new Scanner(System.in);


    public static String generateAnswer() {
        String num1 = "";
        Random random = new Random();
        while (true) {
            //是否重复标志
            boolean flag = false;
            int i = random.nextInt(9999);
            String num = i + "";
            int length1 = num.length();
            if (length1 == 4) {
                //循坏判断数字是否重复
                for (int j = 0; j < length1; j++) {
                    for (int k = j + 1; k < length1; k++) {
                        if (num.charAt(j) == num.charAt(k)) {
                            flag = true;
                            break;
                        }
                    }
                    if (flag == true) {
                        break;
                    }
                }
                if (flag == false) {
                    num1 = i + "";
                    break;
                }
            }
        }
        return num1;
    }

    public static String getPlayerInput(){
        while(true){
            int[] arr = new int[4];
            boolean isRepeat = false;
            for(int temp = 0;temp < arr.length;temp++){
                arr[temp] = scanner.nextInt();
            }
            for (int j = 1; j < arr.length; j++) {
                for (int i = 0; i < j; i++) {
                    if (arr[j] == arr[i]) {
                        System.out.println("请输入四个不重复的数字");
                        isRepeat = true;
                    }
                }
            }
            if(isRepeat){
                continue;
            }
            return "" + arr[0] + arr[1] + arr[2] + arr[3];
        }
    }


    public static String compareGuessAnswer(String num,String guessNum){
        int A = 0;
        int B = 0;
        for (int i = 0; i < guessNum.length(); i++) {
            if(guessNum.charAt(i) == num.charAt(i)){
                A++;
            }
            for (int j = 0; j < num.length(); j++) {
                if(guessNum.charAt(i)==num.charAt(j)){
                    B++;
                }
            }
        }
        System.out.println(A+"A"+B+"B");
        return A+"A"+B+"B";
    }

    public static boolean isWin(String jg){
        return jg.equals("4A4B");
    }

    public static void main(String[] args) {
        while(true){
            System.out.println("是否开始游戏(是/否)");
            String flag = scanner.next();
            if(flag.equals("否")){
                break;
            }else if(!flag.equals("是")){
                System.out.println("请勿输入是/否之外的字符");
                continue;
            }
            String answer = generateAnswer();
            for(int temp = 0;temp < 7;temp++){
                System.out.println("你还有" + (7-temp) + "次机会");
                String input = getPlayerInput();
                if(isWin(compareGuessAnswer(answer,input))){
                    System.out.println("你赢了");
                }else{
                    System.out.println("猜错了");
                }
            }
        }
    }
}
